<?php
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/UserService.php';
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/EmailService.php';
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Context/BaseContext.php';
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/ServiceResult/ServiceResult.php';
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Util/Tool.php';
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../_config/setting/SMTPConfig.php';
    require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../_config/setting/global.setting.php';
    $userService = new UserService();  
    $host = Tool::decode($smtp['host'], "sTunnInVietnAm@2012#");
    $port = Tool::decode($smtp['port'], "sTunnInVietnAm@2012#");
    $user = Tool::decode($smtp['user'], "sTunnInVietnAm@2012#");
    $pass = Tool::decode($smtp['pass'], "sTunnInVietnAm@2012#"); 
	$emailService = new EmailService($host, $port, $user, $pass);
	$emailService->setSiteURL($glb['websiteURL']);
    $success = true;
	if(isset($_POST['username']) && $_POST['username'] != '')
	{
		$uname = $_POST['username'];
		$context = new BaseContext();
		$result = new ServiceResult();
		$rs = $userService->checkUnameExist($context, $result, $uname);
		if($rs == false)
		{
			echo "Username not exist!";
			$success = false;
		}
		if($success == true)
		{
			echo "<script>alert('Processing...');</script>";
			$fromEmail = $glb['contactEmail'];
	    	$emailService->sendEmailForgotPasswordNotification($context, $result, $fromEmail, $uname);
	    	echo "<script>alert('We have just sent email to you, please check your email and complete reset your password.');</script>";
	    	echo "<script>window.location.reload();</script>"; 
		}
		
	}
	else echo "Please enter your username";
?>